package com.yiboshi.rehrs.controller;

import com.alibaba.fastjson.JSONObject;
import com.yiboshi.rehrs.common.VarConstant;
import com.yiboshi.rehrs.domain.entity.ZhongUser;
import com.yiboshi.rehrs.domain.service.IZhongUserService;
import com.yiboshi.rehrs.helper.*;
import com.yiboshi.rehrs.param.ZhongReq;
import com.yiboshi.rehrs.param.ZhongRes;
import com.yiboshi.rehrs.util.HttpClientUtils;
import com.yiboshi.rehrs.util.OkHttpUtil;
import com.yiboshi.rehrs.util.WebUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @author jack 2025/3/21
 */
@RestController
@RequestMapping("/test/")
@Slf4j
public class AntTestController {


    @Autowired
    ProxyHelper proxyHelper;


    @ApiOperation(value = "1.0 test sub", httpMethod = "POST", notes = "1.0 业务测试操作sub", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("submit")
    public ZhongRes submit(@RequestParam Integer used,@RequestParam String url) throws Exception {
        HttpHost proxy;
        if( Objects.equals(used,1)){
            proxy = proxyHelper.one();
        } else {
            proxy = null;
        }
        String execute = new OkHttpUtil().proxySupplier(() -> proxy)
                .url(url).get().execute();
        JSONObject jsonObject = JSONObject.parseObject(execute);
        log.info("url:{} proxy:{} ,jsonObject:{}",url,proxy,jsonObject);
        return ZhongRes.ok(jsonObject);
    }


    @ApiOperation(value = "2.0 test rec", httpMethod = "GET", notes = "2.0 业务测试操作rec", consumes = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("receive")
    public ZhongRes receive(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            Enumeration<String> headerValues = request.getHeaders(name);
            List<String> array = new ArrayList<>();
            while(headerValues.hasMoreElements()){
                array.add(headerValues.nextElement());
            }
            log.info("name:{},value::{}" ,name,String.join(",",array));
        }
        String ipAddress = WebUtils.getIpAddress(request);
        log.info("ipAddress:{}",ipAddress);
        return ZhongRes.ok();
    }
}
